package com.weiquan.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.weiquan.entity.CommunityPost;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 社区帖子Mapper接口
 */
@Mapper
public interface CommunityPostMapper extends BaseMapper<CommunityPost> {

    /**
     * 分页查询帖子列表（包含用户信息）
     */
    @Select("SELECT p.*, u.nickname, u.avatar " +
            "FROM community_posts p " +
            "LEFT JOIN sys_user u ON p.user_id = u.id " +
            "WHERE p.is_deleted = 0 AND p.audit_status = 1 " +
            "AND (#{category} IS NULL OR p.category = #{category}) " +
            "AND (#{keyword} IS NULL OR p.title LIKE CONCAT('%', #{keyword}, '%') OR p.content LIKE CONCAT('%', #{keyword}, '%')) " +
            "ORDER BY p.is_top DESC, p.create_time DESC")
    IPage<CommunityPost> selectPostsWithUser(Page<CommunityPost> page, 
                                            @Param("category") Integer category, 
                                            @Param("keyword") String keyword);

    /**
     * 获取热门帖子
     */
    @Select("SELECT p.*, u.nickname, u.avatar " +
            "FROM community_posts p " +
            "LEFT JOIN sys_user u ON p.user_id = u.id " +
            "WHERE p.is_deleted = 0 AND p.audit_status = 1 " +
            "ORDER BY (p.like_count * 2 + p.comment_count + p.view_count) DESC " +
            "LIMIT #{limit}")
    List<CommunityPost> selectHotPosts(@Param("limit") Integer limit);

    /**
     * 获取精选案例
     */
    @Select("SELECT p.*, u.nickname, u.avatar " +
            "FROM community_posts p " +
            "LEFT JOIN sys_user u ON p.user_id = u.id " +
            "WHERE p.is_deleted = 0 AND p.audit_status = 1 AND p.is_featured = 1 AND p.status = 1 " +
            "ORDER BY p.create_time DESC " +
            "LIMIT #{limit}")
    List<CommunityPost> selectFeaturedCases(@Param("limit") Integer limit);

    /**
     * 增加浏览数
     */
    @Update("UPDATE community_posts SET view_count = view_count + 1 WHERE id = #{id}")
    int increaseViewCount(@Param("id") Long id);

    /**
     * 增加点赞数
     */
    @Update("UPDATE community_posts SET like_count = like_count + 1 WHERE id = #{id}")
    int increaseLikeCount(@Param("id") Long id);

    /**
     * 减少点赞数
     */
    @Update("UPDATE community_posts SET like_count = like_count - 1 WHERE id = #{id} AND like_count > 0")
    int decreaseLikeCount(@Param("id") Long id);

    /**
     * 更新评论数
     */
    @Update("UPDATE community_posts SET comment_count = (SELECT COUNT(*) FROM community_comments WHERE post_id = #{id} AND is_deleted = 0) WHERE id = #{id}")
    int updateCommentCount(@Param("id") Long id);

    /**
     * 获取用户发布的帖子
     */
    @Select("SELECT * FROM community_posts " +
            "WHERE user_id = #{userId} AND is_deleted = 0 " +
            "ORDER BY create_time DESC")
    List<CommunityPost> selectByUserId(@Param("userId") Long userId);

    /**
     * 统计各分类帖子数量
     */
    @Select("SELECT category, COUNT(*) as count " +
            "FROM community_posts " +
            "WHERE is_deleted = 0 AND audit_status = 1 " +
            "GROUP BY category")
    List<Object> selectCategoryStats();
}